package com.example.mymusicdisplay.view;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.mymusicdisplay.R;
import com.example.mymusicdisplay.data.MusicData;
import com.example.mymusicdisplay.databinding.FragmentUserCenterBinding;
import com.example.mymusicdisplay.utils.MusicUtils;

import java.util.ArrayList;
import java.util.List;

public class UserCenterFragment extends Fragment {

    private static final String TAG = "UserCenterFragment";

    private FragmentUserCenterBinding mUserCenterBinding;

    public UserCenterFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: ");
        // Inflate the layout for this fragment
        mUserCenterBinding = DataBindingUtil.inflate(
            LayoutInflater.from(requireContext()), R.layout.fragment_user_center, container, false);
        ViewCompat.setOnApplyWindowInsetsListener(mUserCenterBinding.scrollView2, new OnApplyWindowInsetsListener() {
            @Override
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                layoutParams.topMargin = insets.getSystemWindowInsetTop();
                return insets;
            }
        });
        return mUserCenterBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mUserCenterBinding.local.setOnClickListener(view -> {
            List<MusicData> musicData = MusicUtils.getMusicData(requireContext());
            Log.d(TAG, "onActivityCreated: musicData = " + musicData);
            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("musicList", (ArrayList<? extends Parcelable>) musicData);
            NavController navController = Navigation.findNavController(view);
            navController.navigate(R.id.action_mainFragment_to_localMusicFragment, bundle);
        });

    }
}